<template>
  <div class="login_container">
    <div class="login_box">
        <!--  -->
        <div class="avatar_box">
            <img src="../../assets/logo.png">
        </div>
        <!--  -->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules"  label-width="0px" class="login_form">
            <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-unlock"></el-input>
            </el-form-item>
            <el-form-item class="btns">
                <el-button @click="login" type="primary">登入</el-button>
                <el-button @click="resetForm" type="info">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>
<script>
import { login } from '@/api/login'
export default {
  name: 'LoginUser',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登入名称', trigger: 'blur' },
          { min: 8, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async value => {
        console.log('1111111111111111')
        if (!value) return false
        console.log('2222222222222')
        const res = await login(this.loginForm)
        this.$store.commit('user/setUserInfo', res.data)
        this.$message.success('登入成功')
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container{
    background-color: rgb(12, 100, 134);
    height: 100%;
}
.login_box{
    width: 400px;
    height: 300px;
    background-color: aliceblue;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    .avatar_box{
        height: 130px;
        width: 130px;
        border: 1px solid aquamarine;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: white;
        }
    }
    .login_form{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
    .btns{
        display: flex;
        justify-content: flex-end;
    }
}
</style>
